{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 本文件只是做测试和观察变量情况使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sepallength</th>\n",
       "      <th>sepalwidth</th>\n",
       "      <th>petallength</th>\n",
       "      <th>petalwidth</th>\n",
       "      <th>class</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>5.1</td>\n",
       "      <td>3.5</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>4.9</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>4.7</td>\n",
       "      <td>3.2</td>\n",
       "      <td>1.3</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>4.6</td>\n",
       "      <td>3.1</td>\n",
       "      <td>1.5</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3.6</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   sepallength  sepalwidth  petallength  petalwidth  class\n",
       "0          5.1         3.5          1.4         0.2      0\n",
       "1          4.9         3.0          1.4         0.2      0\n",
       "2          4.7         3.2          1.3         0.2      0\n",
       "3          4.6         3.1          1.5         0.2      0\n",
       "4          5.0         3.6          1.4         0.2      0"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "data = pd.read_csv(r\"dataset/iris.arff.csv\", header=0)\n",
    "# data.drop([\"Id\"], axis=1, inplace=True)\n",
    "data.drop_duplicates(inplace=True)\n",
    "# data[\"class\"].drop_duplicates()\n",
    "# Iris-setosa Iris-versicolor Iris-virginica\n",
    "# 名称映射为数字\n",
    "data[\"class\"] = data[\"class\"].map({\"Iris-setosa\":0, \"Iris-versicolor\":1, \"Iris-virginica\":2})\n",
    "# 筛选数据，只选择0和1\n",
    "data = data[data[\"class\"]!=2]\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'train_X 数据数'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "80"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'train_X 特征数'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 这个部分只是用来研究下结构，课而忽略\n",
    "t1 = data[data[\"class\"]==0]\n",
    "t2 = data[data[\"class\"]==1]\n",
    "t1 = t1.sample(len(t1), random_state=0)\n",
    "t2 = t2.sample(len(t2), random_state=0)\n",
    "train_X = pd.concat([t1.iloc[:40,:-1], t2.iloc[:40,:-1]], axis=0)\n",
    "train_y = pd.concat([t1.iloc[:40,-1], t2.iloc[:40,-1]], axis=0)\n",
    "display(\"train_X 数据数\")\n",
    "display(train_X.shape[0])\n",
    "display(\"train_X 特征数\")\n",
    "display(train_X.shape[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'w_的shape'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "(5,)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'X.asarray 之前的shape'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "(80, 4)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'X.asarray 之后的shape'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "(80, 4)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'X点乘w_后，赋值为z。是一维数组'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "(80,)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'z经过sigmoid后，赋值为p。每一个元素都经过sigmoid计算'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "(80,)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'p的具体值'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([0.66348198, 0.65076419, 0.67125131, 0.6670172 , 0.65107663,\n",
       "       0.66223051, 0.64414488, 0.63812839, 0.66020323, 0.66372196,\n",
       "       0.66245415, 0.67302527, 0.65174082, 0.66272802, 0.65540307,\n",
       "       0.64437407, 0.66169906, 0.68293654, 0.66391723, 0.65794505,\n",
       "       0.61623547, 0.65282384, 0.67448151, 0.68063684, 0.68569089,\n",
       "       0.6492798 , 0.62358668, 0.67252994, 0.6778863 , 0.65679072,\n",
       "       0.65618183, 0.64821449, 0.648357  , 0.6579788 , 0.65350912,\n",
       "       0.65836127, 0.68107142, 0.64807766, 0.68322338, 0.64127447,\n",
       "       0.85483966, 0.84194212, 0.80356871, 0.85725799, 0.87582506,\n",
       "       0.87993872, 0.83145158, 0.81502529, 0.87436588, 0.8645141 ,\n",
       "       0.87722122, 0.85191496, 0.87280602, 0.84708904, 0.86282448,\n",
       "       0.78853212, 0.8118422 , 0.83512585, 0.77793537, 0.80708044,\n",
       "       0.85617469, 0.82081809, 0.82742335, 0.84941882, 0.83236053,\n",
       "       0.86532608, 0.79116714, 0.86224052, 0.86148452, 0.85590049,\n",
       "       0.84957224, 0.83233611, 0.84394779, 0.83460886, 0.85678009,\n",
       "       0.83566901, 0.85711414, 0.84960099, 0.86294575, 0.85972406])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'p经过 p.reshape(-1,1)后的shape'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "(80, 1)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'p的具体值'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[0.66348198],\n",
       "       [0.65076419],\n",
       "       [0.67125131],\n",
       "       [0.6670172 ],\n",
       "       [0.65107663],\n",
       "       [0.66223051],\n",
       "       [0.64414488],\n",
       "       [0.63812839],\n",
       "       [0.66020323],\n",
       "       [0.66372196],\n",
       "       [0.66245415],\n",
       "       [0.67302527],\n",
       "       [0.65174082],\n",
       "       [0.66272802],\n",
       "       [0.65540307],\n",
       "       [0.64437407],\n",
       "       [0.66169906],\n",
       "       [0.68293654],\n",
       "       [0.66391723],\n",
       "       [0.65794505],\n",
       "       [0.61623547],\n",
       "       [0.65282384],\n",
       "       [0.67448151],\n",
       "       [0.68063684],\n",
       "       [0.68569089],\n",
       "       [0.6492798 ],\n",
       "       [0.62358668],\n",
       "       [0.67252994],\n",
       "       [0.6778863 ],\n",
       "       [0.65679072],\n",
       "       [0.65618183],\n",
       "       [0.64821449],\n",
       "       [0.648357  ],\n",
       "       [0.6579788 ],\n",
       "       [0.65350912],\n",
       "       [0.65836127],\n",
       "       [0.68107142],\n",
       "       [0.64807766],\n",
       "       [0.68322338],\n",
       "       [0.64127447],\n",
       "       [0.85483966],\n",
       "       [0.84194212],\n",
       "       [0.80356871],\n",
       "       [0.85725799],\n",
       "       [0.87582506],\n",
       "       [0.87993872],\n",
       "       [0.83145158],\n",
       "       [0.81502529],\n",
       "       [0.87436588],\n",
       "       [0.8645141 ],\n",
       "       [0.87722122],\n",
       "       [0.85191496],\n",
       "       [0.87280602],\n",
       "       [0.84708904],\n",
       "       [0.86282448],\n",
       "       [0.78853212],\n",
       "       [0.8118422 ],\n",
       "       [0.83512585],\n",
       "       [0.77793537],\n",
       "       [0.80708044],\n",
       "       [0.85617469],\n",
       "       [0.82081809],\n",
       "       [0.82742335],\n",
       "       [0.84941882],\n",
       "       [0.83236053],\n",
       "       [0.86532608],\n",
       "       [0.79116714],\n",
       "       [0.86224052],\n",
       "       [0.86148452],\n",
       "       [0.85590049],\n",
       "       [0.84957224],\n",
       "       [0.83233611],\n",
       "       [0.84394779],\n",
       "       [0.83460886],\n",
       "       [0.85678009],\n",
       "       [0.83566901],\n",
       "       [0.85711414],\n",
       "       [0.84960099],\n",
       "       [0.86294575],\n",
       "       [0.85972406]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'p经过np.concatenate([1-p, p], axis=1)拼接后。赋值为r'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "(80, 2)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "\n",
    "# 训练\n",
    "w_ = np.zeros(1 + train_X.shape[1]) \n",
    "z = np.dot(train_X, w_[1:]) + w_[0]\n",
    "p = 1.0 / ( 1.0 + np.exp(-z) )\n",
    "learning_rate = 0.005\n",
    "w_[0] += learning_rate * np.sum(train_y - p) * 1\n",
    "w_[1:] += learning_rate * np.dot(train_X.T, train_y - p)\n",
    "\n",
    "# 预测\n",
    "display(\"w_的shape\")\n",
    "display(w_.shape)\n",
    "display(\"X.asarray 之前的shape\")\n",
    "display(train_X.shape)\n",
    "X = np.asarray(train_X)\n",
    "display(\"X.asarray 之后的shape\")\n",
    "display(X.shape)\n",
    "z = np.dot(X, w_[1:]) + w_[0]\n",
    "display(\"X点乘w_后，赋值为z。是一维数组\")\n",
    "display(z.shape)\n",
    "p = 1.0 / ( 1.0 + np.exp(-z) )\n",
    "display(\"z经过sigmoid后，赋值为p。每一个元素都经过sigmoid计算\")\n",
    "display(p.shape)\n",
    "display(\"p的具体值\")\n",
    "display(p)\n",
    "# 将预测结果转换为二位数组，方便后续拼接\n",
    "p = p.reshape(-1,1)\n",
    "display(\"p经过 p.reshape(-1,1)后的shape\")\n",
    "display(p.shape)\n",
    "display(\"p的具体值\")\n",
    "display(p)\n",
    "# 将两个数组拼接，方向是横向拼接\n",
    "r = np.concatenate([1-p, p], axis=1)\n",
    "display(\"p经过np.concatenate([1-p, p], axis=1)拼接后。赋值为r\")\n",
    "display(r.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "display(\"r的值\")\n",
    "display(r) #横向拼接为为1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# reshape(-1,1)前后的差别\n",
    "* 前：array([0.66348198, 0.65076419])\n",
    "* 后：array([[0.66348198], [0.65076419]])\n",
    "所以是一维数组和二位数组的差别"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
